package com.waypolice.manager.service.impl;

import com.waypolice.manager.service.UpmsArchivesTypesService;
import com.waypolice.manager.service.YearArchiveHappenService;
import com.waypolice.mapper.CmsJudItemMapper;
import com.waypolice.mapper.CmsJudVolumeMapper;
import com.waypolice.mapper.CmsSinItemMapper;
import com.waypolice.mapper.handwriting.YearArchiveHappenMapperSon;
import com.waypolice.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 各年度归档情况统计
 * @Author wangrang
 * @Date Created in 13:48 2019/4/2
 * @Modified By:
 */
@Service
public class YearArchiveHappenServiceImpl implements YearArchiveHappenService {

    @Autowired
    private CmsJudVolumeMapper cmsJudVolumeMapper;

    @Autowired
    private CmsJudItemMapper cmsJudItemMapper;

    @Autowired
    private CmsSinItemMapper cmsSinItemMapper;

    @Autowired
    private YearArchiveHappenMapperSon yearArchiveHappenMapperSon;

    @Autowired
    private UpmsArchivesTypesService upmsArchivesTypesService;

    @Override
    public String getTotalVolNum(String year,String type) {
        CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
        CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
        criteria.andVolArchiveStatusEqualTo(2);
        if(StringUtils.isNotBlank(year)){
            criteria.andVolArchiveYearEqualTo(year);
        }
        if(StringUtils.isNotBlank(type)){
            criteria.andVolClassEqualTo(type);
        }
        List<CmsJudVolume> cmsJudVolumes = cmsJudVolumeMapper.selectByExample(cmsJudVolumeExample);
        int size = cmsJudVolumes.size();

        return String.valueOf(size);
    }

    @Override
    public String getTotalItemNum(String year,String type) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();

        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria criteria = cmsSinItemExample.createCriteria();
        criteria.andItemArcStatusEqualTo(1L);
        if(StringUtils.isNotBlank(year)){
            stringStringHashMap.put("year",year);
            criteria.andItemExp3EqualTo(year);
        }
        if(StringUtils.isNotBlank(type)){
            stringStringHashMap.put("type",type);
            criteria.andItemExp1EqualTo(type);
        }
        Long volByItemNum = yearArchiveHappenMapperSon.getVolByItemNum(stringStringHashMap);

        List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
        Long aLong = Long.valueOf(cmsSinItems.size());
        String all=String.valueOf(volByItemNum+aLong);
        return all;
    }

    @Override
    public Map<String, Object> getNewYearECharts(String newYear) {
        List<Map<String, Object>> objectsEChartsVol = new ArrayList<>();
        List<Map<String, Object>> objectsEChartsItem = new ArrayList<>();
        List<String> typeNameAll = new ArrayList<>();
        //获取各类型的案卷总数,案件总数
        List<UpmsArchivesTypes> allTypes = upmsArchivesTypesService.findAllTypes();
        for (UpmsArchivesTypes allType : allTypes) {
            String type = allType.getSortNum();
            String typeName = allType.getSortName();
            typeNameAll.add(typeName);
            HashMap<String, Object> volHashMap = new HashMap<>();

            //卷
            String totalVolNum = getTotalVolNum(newYear,type);
            if(StringUtils.isBlank(totalVolNum)){
                totalVolNum="0";
            }
            volHashMap.put("value",Integer.valueOf(totalVolNum));
            volHashMap.put("name",typeName);
            objectsEChartsVol.add(volHashMap);

            //件
            HashMap<String, Object> itemHashMap = new HashMap<>();
            String totalItemNum = getTotalItemNum(newYear,type);
            if(StringUtils.isBlank(totalItemNum)){
                totalItemNum="0";
            }
            itemHashMap.put("value",Integer.valueOf(totalItemNum));
            itemHashMap.put("name",typeName);
            objectsEChartsItem.add(itemHashMap);
        }
        HashMap<String, Object> objectHashMapECharts = new HashMap<>();
        objectHashMapECharts.put("vol",objectsEChartsVol);
        objectHashMapECharts.put("item",objectsEChartsItem);
        objectHashMapECharts.put("type",typeNameAll);

        return objectHashMapECharts;
    }

    @Override
    public Map<String, Object> getManyYearsECharts(String yearCountStart, String yearCountEnd) {
        Integer integerStart = Integer.valueOf(yearCountStart);
        Integer integerEnd = Integer.valueOf(yearCountEnd);
        ArrayList<String> obje = new ArrayList<>();
        for (int i = integerStart; i <= integerEnd; i++) {
            obje.add(String.valueOf(i));
        }
        String years = StringUtils.join(obje, "\',\'");
        years="\'"+years+"\'";
        //获取各类型的案卷总数,案件总数
        List<UpmsArchivesTypes> allTypesByYears = upmsArchivesTypesService.findAllTypes();
        List<Map<String, Object>> manyYearsEChartsVol = new ArrayList<>();
        List<Map<String, Object>> manyYearsEChartsItem = new ArrayList<>();
        ArrayList<String> typeAllName = new ArrayList<>();
        for (UpmsArchivesTypes allTypesByYear : allTypesByYears) {
            String sortNum = allTypesByYear.getSortNum();
            String sortName = allTypesByYear.getSortName();
            typeAllName.add(sortName);
            //卷
            HashMap<String, Object> volYearsHashMap = new HashMap<>();
            CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
            CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
            criteria.andVolArchiveStatusEqualTo(2);
            criteria.andVolClassEqualTo(sortNum);
            criteria.andVolArchiveYearIn(obje);
            List<CmsJudVolume> cmsJudVolumes = cmsJudVolumeMapper.selectByExample(cmsJudVolumeExample);
            int size = cmsJudVolumes.size();
            volYearsHashMap.put("value",Integer.valueOf(size));
            volYearsHashMap.put("name",sortName);
            manyYearsEChartsVol.add(volYearsHashMap);

            //件
            HashMap<String, Object> itemYearsHashMap = new HashMap<>();
            CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
            CmsSinItemExample.Criteria criteria1 = cmsSinItemExample.createCriteria();
            criteria1.andItemArcStatusEqualTo(1L);
            criteria1.andItemExp3In(obje);
            criteria1.andItemExp1EqualTo(sortNum);
            List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
            int size1 = cmsSinItems.size();

            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("type","\'"+sortNum+"\'");
            stringStringHashMap.put("years",years);
            Long volByItemNumManyYears = yearArchiveHappenMapperSon.getVolByItemNumManyYears(stringStringHashMap);
            long l = volByItemNumManyYears + Long.valueOf(size1);
            itemYearsHashMap.put("value",l);
            itemYearsHashMap.put("name",sortName);
            manyYearsEChartsItem.add(itemYearsHashMap);
        }

        HashMap<String, Object> yearsHashMapECharts = new HashMap<>();
        yearsHashMapECharts.put("vol",manyYearsEChartsVol);
        yearsHashMapECharts.put("item",manyYearsEChartsItem);
        yearsHashMapECharts.put("type",typeAllName);
        return yearsHashMapECharts;
    }
}
